@echo off
setlocal EnableDelayedExpansion

:: 切换到项目根目录
cd /d %~dp0\..

:: 版本号
set VERSION=v0.1.0
set BINARY_NAME=gitlog

:: 创建发布目录
if not exist release (
    echo Creating release directory...
    mkdir release
)

echo.
echo ============================================================
echo Building %BINARY_NAME% %VERSION%
echo ============================================================
echo.

:: 构建 Windows 版本
echo Building Windows versions...
echo ------------------------------------------------------------
set GOOS=windows

echo Building windows-386...
set GOARCH=386
go build -o release\%BINARY_NAME%-windows-386.exe
if !errorlevel! neq 0 (
    echo Failed to build windows-386 version
    exit /b !errorlevel!
)

echo Building windows-amd64...
set GOARCH=amd64
go build -o release\%BINARY_NAME%-windows-amd64.exe
if !errorlevel! neq 0 (
    echo Failed to build windows-amd64 version
    exit /b !errorlevel!
)

echo Building windows-arm64...
set GOARCH=arm64
go build -o release\%BINARY_NAME%-windows-arm64.exe
if !errorlevel! neq 0 (
    echo Failed to build windows-arm64 version
    exit /b !errorlevel!
)

:: 构建 Linux 版本
echo.
echo Building Linux versions...
echo ------------------------------------------------------------
set GOOS=linux

echo Building linux-386...
set GOARCH=386
go build -o release\%BINARY_NAME%-linux-386
if !errorlevel! neq 0 (
    echo Failed to build linux-386 version
    exit /b !errorlevel!
)

echo Building linux-amd64...
set GOARCH=amd64
go build -o release\%BINARY_NAME%-linux-amd64
if !errorlevel! neq 0 (
    echo Failed to build linux-amd64 version
    exit /b !errorlevel!
)

echo Building linux-arm64...
set GOARCH=arm64
go build -o release\%BINARY_NAME%-linux-arm64
if !errorlevel! neq 0 (
    echo Failed to build linux-arm64 version
    exit /b !errorlevel!
)

:: 构建 MacOS 版本
echo.
echo Building MacOS versions...
echo ------------------------------------------------------------
set GOOS=darwin

echo Building darwin-amd64...
set GOARCH=amd64
go build -o release\%BINARY_NAME%-darwin-amd64
if !errorlevel! neq 0 (
    echo Failed to build darwin-amd64 version
    exit /b !errorlevel!
)

echo Building darwin-arm64...
set GOARCH=arm64
go build -o release\%BINARY_NAME%-darwin-arm64
if !errorlevel! neq 0 (
    echo Failed to build darwin-arm64 version
    exit /b !errorlevel!
)

:: 创建发布包目录结构
echo.
echo Creating release packages...
echo ------------------------------------------------------------

:: Windows 版本打包
for %%a in (386 amd64 arm64) do (
    echo Creating Windows %%a package...
    mkdir "release\%BINARY_NAME%-%VERSION%-windows-%%a"
    copy "release\%BINARY_NAME%-windows-%%a.exe" "release\%BINARY_NAME%-%VERSION%-windows-%%a\%BINARY_NAME%.exe"
    copy "scripts\install.bat.template" "release\%BINARY_NAME%-%VERSION%-windows-%%a\install.bat"
    copy "scripts\uninstall.bat.template" "release\%BINARY_NAME%-%VERSION%-windows-%%a\uninstall.bat"
    cd release
    powershell -Command "Compress-Archive -Path '%BINARY_NAME%-%VERSION%-windows-%%a' -DestinationPath '%BINARY_NAME%-%VERSION%-windows-%%a.zip' -Force"
    cd ..
    rd /s /q "release\%BINARY_NAME%-%VERSION%-windows-%%a"
)

:: Linux 版本打包
where tar >nul 2>nul
if %ERRORLEVEL% EQU 0 (
    for %%a in (386 amd64 arm64) do (
        echo Creating Linux %%a package...
        mkdir "release\%BINARY_NAME%-%VERSION%-linux-%%a"
        copy "release\%BINARY_NAME%-linux-%%a" "release\%BINARY_NAME%-%VERSION%-linux-%%a\%BINARY_NAME%"
        copy "scripts\install.sh.template" "release\%BINARY_NAME%-%VERSION%-linux-%%a\install.sh"
        copy "scripts\uninstall.sh.template" "release\%BINARY_NAME%-%VERSION%-linux-%%a\uninstall.sh"
        cd release
        tar -czf %BINARY_NAME%-%VERSION%-linux-%%a.tar.gz %BINARY_NAME%-%VERSION%-linux-%%a
        cd ..
        rd /s /q "release\%BINARY_NAME%-%VERSION%-linux-%%a"
    )

    :: MacOS 版本打包
    for %%a in (amd64 arm64) do (
        echo Creating MacOS %%a package...
        mkdir "release\%BINARY_NAME%-%VERSION%-darwin-%%a"
        copy "release\%BINARY_NAME%-darwin-%%a" "release\%BINARY_NAME%-%VERSION%-darwin-%%a\%BINARY_NAME%"
        copy "scripts\install.sh.template" "release\%BINARY_NAME%-%VERSION%-darwin-%%a\install.sh"
        copy "scripts\uninstall.sh.template" "release\%BINARY_NAME%-%VERSION%-darwin-%%a\uninstall.sh"
        cd release
        tar -czf %BINARY_NAME%-%VERSION%-darwin-%%a.tar.gz %BINARY_NAME%-%VERSION%-darwin-%%a
        cd ..
        rd /s /q "release\%BINARY_NAME%-%VERSION%-darwin-%%a"
    )
) else (
    echo Warning: tar command not found. Linux and MacOS archives will not be created.
    echo You can manually create them later.
)

:: 删除原始文件
echo.
echo Cleaning up...
echo ------------------------------------------------------------
del /f /q release\%BINARY_NAME%-windows-386.exe
del /f /q release\%BINARY_NAME%-windows-amd64.exe
del /f /q release\%BINARY_NAME%-windows-arm64.exe
del /f /q release\%BINARY_NAME%-linux-386
del /f /q release\%BINARY_NAME%-linux-amd64
del /f /q release\%BINARY_NAME%-linux-arm64
del /f /q release\%BINARY_NAME%-darwin-amd64
del /f /q release\%BINARY_NAME%-darwin-arm64

echo.
echo ============================================================
echo Build completed! Release files are in the 'release' directory:
echo ============================================================
dir /b release\
echo. 